package com.joyo8.app.gdi;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.provider.Settings.Secure;
import android.telephony.TelephonyManager;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.ScrollView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;

public class DeviceInfoActivity extends Activity {
    /** Called when the activity is first created. */
    
    final static String[] MAP_LABEL = new String[]{ "title", "text"};
    //list view adapter
    SimpleAdapter listItemAdapter;
    //list view data source
    ArrayList<HashMap<String, Object>> listItem;  
    ListView myList;
    HardwareInfo mHW;
    Utils mUtils = new Utils();
    AlertDialog infoDialog;
    //the info dialog text
    TextView mDialog_tv;
    //scroll view when text too long
    ScrollView mDialog_sv;
    
    TelephonyManager telephonyManager;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        /*LinearLayout layout = (LinearLayout) findViewById(R.id.layout);
        
        TextView mTv_id_label = new TextView(this);
        mTv_id_label.setTextAppearance(this, android.R.style.TextAppearance_Medium);
        mTv_id_label.setText(R.string.label_device_id);
        layout.addView(mTv_id_label);
        
        TextView mTv_id = new TextView(this);
        mTv_id.setTextAppearance(this, android.R.style.TextAppearance_Small);
        mTv_id.setText(Secure.getString(getContentResolver(), Secure.ANDROID_ID));
        layout.addView(mTv_id);*/
        telephonyManager = (TelephonyManager) this
                .getSystemService(Context.TELEPHONY_SERVICE);
        //infoDialog = new AlertDialog.Builder(this).create();
        listItem = new ArrayList<HashMap<String, Object>>();
        listItemAdapter = new SimpleAdapter(this, listItem, R.layout.list_layout, MAP_LABEL,
                new int[]{R.id.title, R.id.subtitle});
        myList = (ListView)findViewById(R.id.list);
        myList.setAdapter(listItemAdapter);
        
        myList.setOnItemClickListener(listener);
        
        mHW = new HardwareInfo();
        //getTotalMemory();
        initDialogView();
        
        addItems();
        
    }
    
    OnItemClickListener listener = new OnItemClickListener() {

        public void onItemClick(AdapterView<?> adapter, View v, int pos, long id) {
            // TODO Auto-generated method stub
            TextView tv = (TextView) v.findViewById(R.id.title);
            TextView tv2 = (TextView) v.findViewById(R.id.subtitle);
            String _title = tv.getText().toString();
            String _subtitle = tv2.getText().toString();
            mUtils.log(pos + ": " + _title + ":" + id);
            if (getString(R.string.label_mem_info).equals(_title)) {
                mDialog_tv.setText(getTotalMemory());
                showInfoDialog(_title);

            }else{
                mDialog_tv.setText(_subtitle);
                showInfoDialog(_title);
            }
        }
    };
    
    private void initDialogView(){
        mDialog_tv = new TextView(getApplicationContext());
        mDialog_tv.setTextAppearance(getApplicationContext(), android.R.style.TextAppearance_Medium);
        mDialog_sv = new ScrollView(getApplicationContext());
        mDialog_sv.addView(mDialog_tv);
        
        infoDialog = new AlertDialog.Builder(DeviceInfoActivity.this).setView(mDialog_sv)
                .setIcon(android.R.drawable.ic_dialog_alert)
                .setNegativeButton(android.R.string.ok, new DialogInterface.OnClickListener(){

                    public void onClick(DialogInterface dialog, int which) {
                        // TODO Auto-generated method stub
                        infoDialog.dismiss();
                    }})
                .create();
    }
    
    private void addItem(String title, String subTitle){
        HashMap<String, Object> map = new HashMap<String, Object>();
        map.put("title", title);
        map.put("text", subTitle);
        listItem.add(map);
        listItemAdapter.notifyDataSetChanged();
    }
    
    private void addItems(){
        
        addItem(getString(R.string.label_mem_info),getString(R.string.msg_detail));
        
        addItem("JNI",mHW.stringFromJNI());
        // android id
        addItem(getString(R.string.label_device_id),Secure.getString(getContentResolver(), Secure.ANDROID_ID));
        
        addItem(getString(R.string.label_android_version), android.os.Build.VERSION.RELEASE);
        
        addItem(getString(R.string.label_product_brand), android.os.Build.BRAND);
        
        addItem(getString(R.string.label_cpu), android.os.Build.CPU_ABI);
        
        addItem("CPU-name", getCpuInfo()[0]);
        
        addItem("CPU-feq", getCpuInfo()[1]);
        
        addItem(getString(R.string.label_device_indstrial_name),android.os.Build.DEVICE);
        
        addItem(getString(R.string.label_build_number),android.os.Build.DISPLAY);
        
        addItem(getString(R.string.label_fingerprint),android.os.Build.FINGERPRINT);
        
        addItem("HOST", android.os.Build.HOST);
        
        addItem("ID",android.os.Build.ID);
        
        addItem(getString(R.string.label_manufactuer),android.os.Build.MANUFACTURER);
        
        addItem(getString(R.string.label_product_model), android.os.Build.MODEL);
        
        addItem(getString(R.string.label_product_name), android.os.Build.PRODUCT);
        
        addItem(getString(R.string.label_build_tags), android.os.Build.TAGS);
        
        addItem(getString(R.string.label_build_type), android.os.Build.TYPE);
        
        addItem(getString(R.string.label_build_user), android.os.Build.USER);
        
        addItem(getString(R.string.label_version_codename), android.os.Build.VERSION.CODENAME);
        
        addItem(getString(R.string.label_version_incremental), android.os.Build.VERSION.INCREMENTAL);
        
        addItem(getString(R.string.label_baseband_version),getBaseBand());
        
        addItem(getString(R.string.label_imei), getIMEI());
        
        addItem(getString(R.string.label_phone_number), telephonyManager.getLine1Number());
        
        addItem(getString(R.string.label_network_name), getNetworkOperatorName());
        
        addItem(getString(R.string.label_network_type), getNetworkType());
    }
    
    /**
     * get android phone baseband
     * @return
     */
    private String getBaseBand(){
        String _ret = "";
        try { 
            Class cl = Class.forName("android.os.SystemProperties"); 
            Object invoker = cl.newInstance(); 
            Method m = cl.getMethod("get", new Class[] { String.class,String.class }); 
            Object result = m.invoke(invoker, new Object[]{"gsm.version.baseband", "no message"}); 
            _ret = (String)result; 
        } catch (Exception e) { 
        }
        return _ret;
    }
    
    /**
     * get mobile IMEI number
     * @return
     */
    private String getIMEI() {
        String _ret = "";
        try {
            _ret = telephonyManager.getDeviceId();
        } catch (Exception e) {

        }
        return _ret;
    }
    
    /**
     * get mobile network operator name, like: CMCC 
     * @return
     */
    private String getNetworkOperatorName() {
        String _ret = "";
        try {
            _ret = telephonyManager.getNetworkOperatorName();
        } catch (Exception e) {

        }
        return _ret;
    }
    
    /**
     * get mobile network type
     * @return
     */
    private String getNetworkType(){
        String _ret = "";
        try {
            int _type = telephonyManager.getNetworkType();
            switch(_type){
                case TelephonyManager.NETWORK_TYPE_1xRTT:
                    _ret = "1xRTT";
                    break;
                case TelephonyManager.NETWORK_TYPE_CDMA:
                    _ret = "CDMA";
                    break;
                case TelephonyManager.NETWORK_TYPE_EDGE:
                    _ret = "EDGE";
                    break;
                case TelephonyManager.NETWORK_TYPE_EVDO_0:
                    _ret = "EVDO_0";
                    break;
                case TelephonyManager.NETWORK_TYPE_EVDO_A:
                    _ret = "EVDO_A";
                    break;
                case TelephonyManager.NETWORK_TYPE_GPRS:
                    _ret = "GPRS";
                    break;
                case TelephonyManager.NETWORK_TYPE_HSDPA:
                    _ret = "HSDPA";
                    break;
                case TelephonyManager.NETWORK_TYPE_HSPA:
                    _ret = "HSPA";
                    break;
                case TelephonyManager.NETWORK_TYPE_HSUPA:
                    _ret = "HSUPA";
                    break;
                case TelephonyManager.NETWORK_TYPE_UMTS:
                    _ret = "UMTS";
                    break;
                    default:
                        _ret = "unknown";
            }
        } catch (Exception e) {

        }
        return _ret;
    }

    public String[] getCpuInfo() {
        String str1 = "/proc/cpuinfo";
        String str2="";
        String[] cpuInfo={"",""};
        String[] arrayOfString;
        try {
            FileReader fr = new FileReader(str1);
            BufferedReader localBufferedReader = new BufferedReader(fr, 8192);
            str2 = localBufferedReader.readLine();
            arrayOfString = str2.split("\\s+");
            for (int i = 2; i < arrayOfString.length; i++) {
                cpuInfo[0] = cpuInfo[0] + arrayOfString[i] + " ";
            }
            str2 = localBufferedReader.readLine();
            arrayOfString = str2.split("\\s+");
            cpuInfo[1] += arrayOfString[2];
            localBufferedReader.close();
        } catch (IOException e) {
        }
        return cpuInfo;
    }
    
    public String getTotalMemory() {
        String str1 = Utils.FILE_MEM;
        String str2="";
        String _ret = "";
        try {
            FileReader fr = new FileReader(str1);
            BufferedReader localBufferedReader = new BufferedReader(fr, 8192);
            while ((str2 = localBufferedReader.readLine()) != null) {
                mUtils.log("---" + str2);
                _ret += str2 +"\r";
            }
        } catch (IOException e) {
        }
        return _ret;
    }
    
    
    private void showInfoDialog(String title){
        if (infoDialog == null) {
            infoDialog = new AlertDialog.Builder(DeviceInfoActivity.this).setView(mDialog_sv)
                    .setTitle(title)
                    .show();
        } else {
            infoDialog.setTitle(title);
            infoDialog.show();
        }
    }


}